package es.udc.emarketapp.http.controller.actions.products;

import es.udc.emarketapp.emarket.model.productFacade.ProductFacade;
import es.udc.emarketapp.emarket.model.productFacade.vo.ProductChunkVO;
import es.udc.emarketapp.emarket.model.seller.vo.SellerVO;
import es.udc.emarketapp.emarket.model.usersFacade.UsersFacade;
import es.udc.emarketapp.http.view.actionForms.FindSellerProductsForm;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class FindSellerProductsAction extends DefaultAction {

    public final static String PRODUCTS_AND_SELLER_DATA = "PRODUCTS_AND_SELLER_DATA";
    public final static String SELLER_PRODUCTS = "SELLER_PRODUCTS";

    public ActionForward doExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        /* Get data. */
        FindSellerProductsForm findForm = (FindSellerProductsForm) form;
        String idSeller = findForm.getIdSeller();
        String type = findForm.getType();
        int startIndex = findForm.getStartIndex();
        int count = findForm.getCount();




        ProductFacade productFacade = new ProductFacade();
        ProductChunkVO productChunkVO = productFacade.findBySeller(idSeller, startIndex, count);


        if (productChunkVO.getProductVOs().size() > 0) {
            request.setAttribute("products",
                    productChunkVO.getProductVOs());
        }

        /* Generate parameters for previous and next links. */
        Map previousLinkParameters = getPreviousLinkParameters(type, idSeller,
                startIndex, count);


        if (previousLinkParameters != null) {
            request.setAttribute("previous", previousLinkParameters);

        }

        if (productChunkVO.getExistMoreProducts()) {
            Map nextLinkParameters = getNextLinkParameters(type, idSeller,
                    startIndex, count);
            request.setAttribute("next", nextLinkParameters);

        }


        request.setAttribute("action", SELLER_PRODUCTS);
       /*
        request.setAttribute("type", type);
        request.setAttribute("idSeller", idSeller);

        /* Return ActionForwards. */


        if (type.equals(PRODUCTS_AND_SELLER_DATA)) {

            UsersFacade usersFacade = new UsersFacade();
            SellerVO sellerPublicDetails = null;
            try {
                SellerVO sellerVO = usersFacade.findSellerById(idSeller);

                sellerPublicDetails = new SellerVO(sellerVO.getIdVendedor(),
                        null, null, null, null, null, sellerVO.getDir(),
                        sellerVO.getCiudad(), sellerVO.getCp(), sellerVO.getProv(),
                        sellerVO.getPais(), sellerVO.getEmail(),
                        sellerVO.getTlf1(), sellerVO.getTlf2(), null, true, true,
                        sellerVO.getNomPila(), null, sellerVO.getFax(), null, null);


            } catch (InstanceNotFoundException e) {
                ActionMessages errors = new ActionMessages();
                errors.add("identifier",
                        new ActionMessage("ErrorMessages.seller.notFound"));
                saveErrors(request, errors);
                return mapping.findForward("failure");
            }

            request.setAttribute("seller", sellerPublicDetails);
            return mapping.findForward("productsAndSellerData");

        } else {
            return mapping.findForward("products");
        }


    }

    private Map getPreviousLinkParameters(String type, String idSeller,
            int startIndex, int count) {

        Map linkAttributes = null;

        if ((startIndex - count) > 0) {

            linkAttributes = getCommonPreviousNextLinkParameters(type, idSeller,
                    startIndex, count);
            linkAttributes.put("startIndex", new Integer(startIndex - count));

        }

        return linkAttributes;


    }

    private Map getNextLinkParameters(String type, String idSeller, int startIndex,
            int count) {

        Map linkAttributes = getCommonPreviousNextLinkParameters(type, idSeller,
                startIndex, count);

        linkAttributes.put("startIndex", new Integer(startIndex + count));

        return linkAttributes;
    }

    private Map getCommonPreviousNextLinkParameters(String type, String idSeller,
            int startIndex, int count) {

        Map linkAttributes = new HashMap();

        linkAttributes.put("type", type);
        linkAttributes.put("idSeller", idSeller);
        linkAttributes.put("count", new Integer(count));

        return linkAttributes;

    }
}
